﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using System.Collections;

namespace BLL
{
    public class ClassService
    {
        private ClassesDAL classesDAL = new ClassesDAL();
        private StudentService students = new StudentService();
        public void CreateClass(BoClasses cla)
        {
            cla.CLA_ID = System.Guid.NewGuid().ToString();
            classesDAL.Insert(cla);
        }

        public void UpdateClass(BoClasses cla)
        {
            classesDAL.Update(cla);
            
        }

        /// <summary>
        /// 根据班级id来删除班级
        /// </summary>
        /// <param name="cLA_ID"></param>
        public void DeleteClass(string cLA_ID)
        {
            if (new DbCommon.DbHelper().GetCount("Students", "STU_CLASSES='" + cLA_ID + "' and STU_IsShow ='true'") > 0)
            {
                System.Windows.Forms.MessageBox.Show("该班级下还有学生，请先删除学生，再删除班级");
            }
            else
            {
                classesDAL.Delete(cLA_ID);
            }
        }
        /// <summary>
        /// 批量删除班级,相应的删除此班级的学生
        /// </summary>
        /// <param name="classesArray">班级id的数组</param>
        /// <returns>返回受影响的行数</returns>
        //public int DeleteMoreClasses(ArrayList classesArray)
        //{
        //    students.DeleteMoreByClassArr(classesArray);
        //    return classesDAL.DeleteMoreClasses(classesArray);
        //}

        /// <summary>
        /// 通过学院的id来删除班级
        /// </summary>
        /// <param name="academyId">学院id</param>
        public void DeleteClassesByAcademyId(string academyId)
        {
            BLL.StudentService studentservice = new StudentService();
            IList<BoClasses> classeslist =  getClassesByAcademy(academyId);
            classesDAL.DeleteClassesByAcademyId(academyId);
            foreach (BoClasses model in classeslist)
            {
                studentservice.DeleteByClssId(model.CLA_ID);
            }
        }

        
        /// <summary>
        ///  通过专业的id来删除班级
        /// </summary>
        /// <param name="majorId"></param>
        public void DeleteClassesByMajorId(string majorId)
        {
            BLL.StudentService studentservice = new StudentService();
            IList<BoClasses> classeslist = getClassesByMajor(majorId);
            classesDAL.DeleteClassesByMajorId(majorId);
            foreach (BoClasses model in classeslist)
            {
                studentservice.DeleteByClssId(model.CLA_ID);
            }
        }
        public BoClasses getClass(string id)
        {
            return classesDAL.GetModel(id);
        }

        public IList<BoClasses> getClassesByAcademy(string academyId)
        {
            return classesDAL.GetClassesByAcademy(academyId); 
        }
        public IList<BoClasses> getClassesByMajor(string majorId)
        { 
            return  classesDAL.GetClassesByMajor(majorId);
        }
        public List<BoClasses> GetAllClasses(object sqlcon)
        {
            return classesDAL.GetAllClasses(sqlcon);    
        }

        /// <summary>
        /// 得到分页的数据列表
        /// </summary>
        /// <param name="orderCon">排序条件</param>
        /// <param name="sqlCon">查询条件</param>
        /// <param name="pagesize">每页显示大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="records">返回查询到的记录</param>
        /// <returns>返回对象列表</returns>
        public IList<BoClasses> getPageCompetitions(string sqlCon, string orderCon, int pagesize, int pageIndex, out int records)
        {
            return classesDAL.getPageCompetitions(sqlCon, orderCon, pagesize, pageIndex, out records);
        }
          /// <summary>
        /// 通过班级编号来获取班级id
        /// </summary>
        /// <param name="ClassCode"></param>
        /// <returns></returns>
        public string GetIdByCode(string ClassCode)
        {
            return classesDAL.GetIdByName(ClassCode); 
        }

        /// <summary>
        /// 通过班级编号以及专业编号来获取相应的对象
        /// </summary>
        /// <param name="ClassCode">班级编号</param>
        /// <param name="MajorCode">专业编号</param>
        /// <returns>返回获得的对象</returns>
        public BoClasses GetClassByCodeAndMajorCodeAndAcademyName(string ClassCode, string MajorCode, string AcademyName)
        {
            return classesDAL.GetClassByCodeAndMajorCodeAndAcademyName(ClassCode, MajorCode, AcademyName);
        }
    }
}
